package com.mapaower.project.controller;

import com.mapaower.project.entity.ProjectApplication;
import com.mapaower.project.service.ProjectApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 项目报名控制器
 * 处理项目报名相关的HTTP请求
 */
@RestController
@RequestMapping("/api/applications")
public class ProjectApplicationController {

    @Autowired
    private ProjectApplicationService applicationService;

    /**
     * 创建项目报名
     */
    @PostMapping
    public ResponseEntity<ProjectApplication> createApplication(@RequestBody ProjectApplication application) {
        ProjectApplication createdApplication = applicationService.createApplication(application);
        return new ResponseEntity<>(createdApplication, HttpStatus.CREATED);
    }

    /**
     * 更新项目报名信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<ProjectApplication> updateApplication(@PathVariable Long id, @RequestBody ProjectApplication application) {
        application.setId(id);
        ProjectApplication updatedApplication = applicationService.updateApplication(application);
        return ResponseEntity.ok(updatedApplication);
    }

    /**
     * 根据ID获取项目报名详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<ProjectApplication> getApplicationById(@PathVariable Long id) {
        Optional<ProjectApplication> application = applicationService.getApplicationById(id);
        return application.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    /**
     * 根据项目ID获取报名列表
     */
    @GetMapping("/project/{projectId}")
    public ResponseEntity<List<ProjectApplication>> getApplicationsByProjectId(@PathVariable Long projectId) {
        List<ProjectApplication> applications = applicationService.getApplicationsByProjectId(projectId);
        return ResponseEntity.ok(applications);
    }

    /**
     * 根据开发人员ID获取报名列表
     */
    @GetMapping("/developer/{developerId}")
    public ResponseEntity<List<ProjectApplication>> getApplicationsByDeveloperId(@PathVariable Long developerId) {
        List<ProjectApplication> applications = applicationService.getApplicationsByDeveloperId(developerId);
        return ResponseEntity.ok(applications);
    }

    /**
     * 检查开发人员是否已报名某个项目
     */
    @GetMapping("/check")
    public ResponseEntity<Boolean> checkApplication(@RequestParam Long projectId, @RequestParam Long developerId) {
        boolean hasApplied = applicationService.hasApplied(projectId, developerId);
        return ResponseEntity.ok(hasApplied);
    }

    /**
     * 更新项目报名状态
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<ProjectApplication> updateApplicationStatus(@PathVariable Long id, @RequestParam ProjectApplication.ApplicationStatus status) {
        ProjectApplication updatedApplication = applicationService.updateApplicationStatus(id, status);
        return ResponseEntity.ok(updatedApplication);
    }

    /**
     * 撤回项目报名
     */
    @PutMapping("/{id}/withdraw")
    public ResponseEntity<ProjectApplication> withdrawApplication(@PathVariable Long id) {
        ProjectApplication updatedApplication = applicationService.withdrawApplication(id);
        return ResponseEntity.ok(updatedApplication);
    }

    /**
     * 删除项目报名
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteApplication(@PathVariable Long id) {
        applicationService.deleteApplication(id);
        return ResponseEntity.noContent().build();
    }
}